 <!doctype html>
 <html lang="zh-CN">
 <head>
   <meta charset="UTF-8">
   <link rel="stylesheet" href="css/common.css">
   <link rel="stylesheet" href="css/main.css">
   <link rel="stylesheet" href="css/jquery-ui.css">
   <link rel="stylesheet" href="css/jquery-impromptu.min.css">
   <title>Document</title>
   <style>

   </style>
 </head>
 <body>
 <div id="dataLoad" style="display:none; position: fixed; width: 100%; top: 20%; z-index: 1001;">
        <table width=100% height=100% border=0 align=center valign=middle>
         <tr height=50%><td align=center>&nbsp;</td></tr>
         <tr><td align=center><img src="images/loading.gif"/></td></tr>
         <tr><td align=center id="loading_text">提交中，请稍后......</td></tr>
         <tr height=50%><td align=center>&nbsp;</td></tr>
        </table>
      </div>
 <div id="search_bar" class="mt10">
     <div class="box">
         <div class="box_border">
             <div class="box_top"><b class="pl15">数据筛选</b></div>
             <div class="box_center pt10 pb10" style="padding-left: 35%;">
                 <table class="form_table" border="0" cellpadding="0" cellspacing="0">
                     <tr>
                         <td>当前状态</td>
                         <td>
                             <span class="fl">
                                 <div class="select_border">
                                     <div class="select_containers ">
                                         <select name="" class="select" id="approve_state">
                                             <option value="4">全部</option>
                                             <option value="0">待审批</option>
                                             <option value="1">已同意</option>
                                             <option value="2">已驳回</option>
                                             <option value="3">待报销</option>
                                         </select>
                                     </div>
                                 </div>
                             </span>
                         </td>
                         <td class="department" style="display:none">所属部门</td>
                         <td class="department" style="display:none">
                             <span class="fl">
                                 <div class="select_border">
                                     <div class="select_containers ">
                                         <select name="" class="select" id="depart-select">
                                         </select>
                                     </div>
                                 </div>
                             </span>
                         </td>
                         <td><input type="button" value="确定" id="confirm_query" class="ext_btn ext_btn_submit"></td>
                     </tr>

                 </table>
             </div>
         </div>
     </div>
 </div>
 <div class="mt10">
     <div class="box">
         <div class="box_border">
             <div class="box_top"><b class="pl15">出差申请信息</b></div>
                 <div class="mt10">
                        <div id="table" class="mt10">
                            <div class="box span10 oh" style="margin-left: auto; margin-right: auto">
                                <table id="approval_info" width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table" style="table-layout:fixed; font-size:1.2rem">
                                         <tr>
                                             <th class="td_center" style="width:5%">编号</th>
                                             <th class="td_center" style="width:15%">出差目的地</th>
                                             <th class="td_center" style="width:15%">出差类别</th>
                                             <th class="td_center" style="width:15%">大案要案</th>
                                             <th class="td_center" style="width:15%">申请时间</th>
                                             <th class="td_center" style="width:10%">当前状态</th>
                                             <th class="td_center" style="width:25%">可选操作</th>
                                         </tr>
                                </table>
                                <div class="page mt10">
                                    <div class="pagination">
                                        <ul>
                                            <li class="first-child" ><a href="#" id="firstPage">首页</a></li>
                                            <li class="" ><a href="#" id="prevPage">上一页</a></li>
                                            <li class="" ><a href="#" id="nextPage">下一页</a></li>
                                            <li class="last-child " ><a href="#" id="endPage">末页</a></li>
                                            <li class="disabled pagedata" id="page1"><a id="pageInfo">1/20</a></li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                 </div>
         </div>
     </div>
 </div>
 <div id="app_print_dlg" class="mt10" style="display:none" title="打印窗口">
     <div class="box">
         <div class="box_center">
             <table class="form_table pb15" id="approval_print_table" width="100%" border="0" cellpadding="0" cellspacing="0">
                 <tr>
                     <td class="td_left">差旅审批单</td>
                     <td>
                         <span class="pl5">
                             <input type="button" id="app_btn" name="button" class="btn1 btn82 btn_export" value="打印">
                         </span>
                     </td>
                 </tr>
                 <tr>
                     <td class="td_left">借款审批单</td>
                     <td>
                         <span class="pl5">
                             <input type="button" id="fee_btn" name="button" class="btn1 btn82 btn_export" value="打印">
                         </span>
                     </td>
                 </tr>
                 <tr>
                     <td class="td_left">借车审批单</td>
                     <td>
                         <span class="pl5">
                             <input type="button" id="car_btn" name="button" class="btn1 btn82 btn_export" value="打印">
                         </span>
                     </td>
                 </tr>
             </table>
         </div>
     </div>
 </div>
 </body>
 <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
 <script type="text/javascript" src="js/jquery-ui.js"></script>
 <script type="text/javascript" src="js/common.js"></script>
 <script type="text/javascript" src="js/config.js"></script>
 <script type="text/javascript" src="js/jquery-impromptu.min.js"></script>
 <script type="text/javascript">
//     //辅助变量，每页显示的信息数
//     var rowPerPage = 20;
//     //辅助变量，当前的页数
//     var curPage = 1;
//     //辅助变量，存放总页数
//     var totalPage = 1;

     var firstSet = true;
    //保存当前是第几条记录的变量

     //辅助函数，处理数据筛选
     function onConfirmOnDataQuery(){
         $('#confirm_query').click(function(e){
             firstSet = true;
             getUserBusinessApprove();

             e.preventDefault();
         })
     }

     //辅助函数，获取当前用户的所有出差审批信息
     function getUserBusinessApprove(){
         //清空当前数据
         $('#loading_text').html('获取中，请稍候......');
         $('#dataLoad').show();
         $('.info_tr').empty();
         var data = JSON.parse(window.localStorage.getItem('user_info'));
        // var type = $('#business_type').val();

         var state = $('#approve_state').val();
         var user_id = data.user_id;
         $.post(url + 'BusinessApprove/GetMyBusinessApprove.php', {'user':user_id, 'state':state, 'pageCount':curPage, 'firstSet': firstSet ? 1 : 0}, function(ret){
             var d = JSON.parse(ret);
             //alert(ret);
             $('#loading_text').html('提交中，请稍后......');
             $('#dataLoad').hide();
             if(d.state == 1){
                 if(firstSet){
                     createTablePage(d.totalCount);
                     firstSet = false;
                 }
                 createApprovalTable(d.data);
             }
             else{
                 $.prompt( "<h3>网络或服务器错误，请稍后再试</h3>",{
                     title: '错误提醒',
                     buttons: {'确定': true}
                 });
             }
         })
     }

    //辅助函数，生成打印对话框
     function initPrintApproveDlg(info){
        if(info.is_loan_car == 0 || (info.is_loan_car == 1 && info.loan_car.state == 2)){
            $('#app_print_dlg tr').eq(2).hide();
        }
        else{
            $('#app_print_dlg tr').eq(2).show();
        }
        if(info.is_loan_money == 0 || (info.is_loan_money == 1 && info.loan_money.state == 2)){
            $('#app_print_dlg tr').eq(1).hide();
        }
        else{
            $('#app_print_dlg tr').eq(1).show();
        }
      //  alert(info.approval_ID);
        $('#app_btn').unbind('click');
        $('#app_btn').click(function(e){
            var pdfURL = url + 'PDF/CreateBusinessApprovePDF.php';
            var approval_ID = info.approval_ID;
            printApprovePDF(pdfURL, approval_ID);
        });
        $('#fee_btn').unbind('click');
        $('#fee_btn').click(function(e){
            var pdfURL = url + 'PDF/CreateLoanFeePDF.php';
            var approval_ID = info.approval_ID;
            printApprovePDF(pdfURL, approval_ID);
        });
        $('#car_btn').unbind('click');
        $('#car_btn').click(function(e){
            var pdfURL = url + 'PDF/CreateLoanCarPDF.php';
            var approval_ID = info.approval_ID;
            printApprovePDF(pdfURL, approval_ID);
        })

     }

    //辅助函数，打印报销审批单,借车审批单，借款审批单
    function printApprovePDF(pdfURL, approval_ID){
         $('#dataLoad').show();
         $.post(pdfURL, {'appID': approval_ID, 'byApp': 1}, function(data){
             var t= JSON.parse(data);
             $('#dataLoad').hide();
             if(t.state == 1){
                 $.prompt("<h3>生成成功</h3>", {
                     title: '操作提醒',
                     buttons: {'确定': true}
                 });
                 window.open(url + t.url);
             }
             else{
                 $.prompt("<h3>网络或数据库错误，请联系管理员</h3>", {
                     title: '错误提醒',
                     buttons: {'确定': true}
                 })
             }
         });
    }

    //辅助函数，获取出差审批信息
    function processBusinessApp(appID, type){
        $('#dataLoad').show();
        $.post(url + 'BusinessApprove/GetBusinessApproveByID.php', {'appID':appID}, function(data){
            $('#dataLoad').hide();
            var t = JSON.parse(data);
            console.log(data);
            if(t.state == 1){
                switch(type){
                    case 1: //修改或者查看
                        window.localStorage.setItem('cur_travel_info', JSON.stringify(t.data[0]));
                        window.localStorage.setItem('approve_state', 0);
                        jumpPage('bussiness_approval_info.html');
                        break;
                    case 2: //报销
                        window.localStorage.setItem('cur_travel_info', JSON.stringify(t.data[0]));
                        window.localStorage.setItem('history_fee_id', '');
                        jumpPage('business_apply_for.html');
                        break;
                }
            }
            else{
                $.prompt("<h3>网络或数据库错误，请稍后再试</h3>", {
                    title: '错误提醒',
                    buttons: {'确定': true}
                })
            }
        })
    }


     function createApprovalTable(info){
         var end = 0;
         end = info.length > rowPerPage ? rowPerPage : info.length;
         for(var i = 0; i < end; i++){
            var case_name = info[i].case_name == "" ? '否':info[i].case_name;
            $('#approval_info').append("" +
                    "<tr class='tr info_tr'>" +
                        "<td class='td_center'>" + ((i+1) + (curPage-1) * rowPerPage) + "</td>" +
                        "<td class='td_center'>" + info[i].destination + "</td>" +
                        "<td class='td_center'>" + getBusinessTypeInfo(info[i].type) + "</td>" +
                        "<td class='td_center'>" + case_name + "</td>" +
                        "<td class='td_center'>" + info[i].approve_time + "</td>" +
                        "<td class='td_center'>" + getApproveState(info[i].state, info[i].state_detail, false) + "</td>" +
                        "<td class='td_center'>" +
                        "<a class='ext_btn browse browse_btn'><span class='browse'></span>查看</a>"+
                        "<a class='ext_btn modify modify_btn'><span class='modify'></span>修改</a>" +
                        "<a class='ext_btn print print_btn'><span class='print'></span>打印</a>" +
                        "<a class='ext_btn baoxiao baoxiao_btn'><span class='baoxiao'></span>报销</a>" +
                        "</td>"+
                    "</tr>");

             if(info[i].state == 0){ //如果是待审批状态，可以查看，不能打印，修改，报销
                 $(".print").eq(i*2).addClass('disabled');
                 $(".print").eq(i*2+1).addClass('disabled');
                 $(".baoxiao").eq(i*2).addClass('disabled');
                 $(".baoxiao").eq(i*2+1).addClass('disabled');
                 $(".modify").eq(i*2).addClass('disabled');
                 $(".modify").eq(i*2+1).addClass('disabled');
             }
             else if(info[i].state == 1){ //如果是已同意状态，不能修改
                 $(".modify").eq(i*2).addClass('disabled');
                 $(".modify").eq(i*2+1).addClass('disabled');
             }
             else if(info[i].state == 2){ //如果是已驳回状态，可以修改重新提交，不能报销、打印
                 $(".print").eq(i*2).addClass('disabled');
                 $(".print").eq(i*2+1).addClass('disabled');
                 $(".baoxiao").eq(i*2).addClass('disabled');
                 $(".baoxiao").eq(i*2+1).addClass('disabled');
             }
             else{  //如果是待报销或已报销状态，只能查看和打印
                 $(".modify").eq(i*2).addClass('disabled');
                 $(".modify").eq(i*2+1).addClass('disabled');
                 $(".baoxiao").eq(i*2).addClass('disabled');
                 $(".baoxiao").eq(i*2+1).addClass('disabled');
             }

        }

        $(".browse_btn, .modify_btn").each(function(index, elem){
            if($(elem).hasClass('disabled')){
                return;
            }
            $(this).click(function(e){
                var id = $(this).parent().parent().children('td').eq(0).html();

            //    window.localStorage.setItem('curItem', JSON.stringify(info[id-1]));
                processBusinessApp(info[id-1].approval_ID, 1);
//                window.localStorage.setItem('approve_state', 0);
//
//
//                jumpPage('bussiness_approval_info.html');
            })
        });
        $(".print_btn").each(function(index, elem){
            if($(elem).hasClass('disabled')){
                return;
            }
            $(this).click(function(){
                var id = $(this).parent().parent().children('td').eq(0).html();
                initPrintApproveDlg(info[index]);
                $('#app_print_dlg').dialog({
                    resizable: false,
                    height: "auto",
                    width: "auto",
                    modal: true,
                    buttons: {
                        '取消': function(){
                            $(this).dialog('close');
                        }
                    }
                })
            })
        });
        $(".baoxiao_btn").each(function(index, elem){
            if($(elem).hasClass('disabled')){
                return;
            }
            $(this).click(function(e){
                var id = $(this).parent().parent().children('td').eq(0).html();
                processBusinessApp(info[id-1].approval_ID, 2);

            })
        })
     }

     $(function(){

//         var agree = window.localStorage.getItem('agree');
//         if(agree){
//             $("#approve_state option[value='1']").attr('selected', 'selected');
//         }
//         else{
//             $("#approve_state option[value='0']").attr('selected', 'selected');
//         }
         getUserBusinessApprove();
         clickFirstPage(getUserBusinessApprove);
         clickEndPage(getUserBusinessApprove);
         clickNextPage(getUserBusinessApprove);
         clickPrevPage(getUserBusinessApprove);

         onConfirmOnDataQuery();

     });
 </script>
 </html>
  